package edu.xaut.bookingsys.service;

import cn.dev33.satoken.util.SaFoxUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xaut.bookingsys.entity.EnumLevel;
import edu.xaut.bookingsys.entity.InfoTeacher;
import edu.xaut.bookingsys.mapper.InfoTeacherMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InfoTeacherService extends ServiceImpl<InfoTeacherMapper, InfoTeacher> {

    /**
     * 取得不包含主任的所有老师信息
     *
     * @return 返回实验室老师和教学老师
     */
    public List<InfoTeacher> getLabTeachersAndNormTeachers() {
        var wrapper = new LambdaQueryWrapper<InfoTeacher>();

        return getBaseMapper().selectList(
                wrapper.and(v -> v
                                .eq(InfoTeacher::getLevel, EnumLevel.LabTeacher).or()
                                .eq(InfoTeacher::getLevel, EnumLevel.NormTeacher))
                        .groupBy(InfoTeacher::getLevel));
    }


    public int insertTeacher(InfoTeacher infoTeacher) {

        return getBaseMapper().insert(infoTeacher);

    }

    // 求或还是求和
    public List<InfoTeacher> searchByNameOrLevel(String name, EnumLevel level) {

        var wrapper = new LambdaQueryWrapper<InfoTeacher>();
        if (SaFoxUtil.isEmpty(level)) {
            return getBaseMapper().selectList(wrapper.like(SaFoxUtil.isNotEmpty(name), InfoTeacher::getName, name));
        }
        if (SaFoxUtil.isEmpty(name)) {
            return getBaseMapper().selectList(wrapper.eq(InfoTeacher::getLevel, level)
                  );

        }
        return getBaseMapper().selectList(wrapper.eq(InfoTeacher::getLevel, level)
                .and(v -> v.like(InfoTeacher::getName, name.trim()))
                .orderByAsc(InfoTeacher::getLevel));


    }


    public List<InfoTeacher> getAllTeachers() {
        var wrapper = new LambdaQueryWrapper<InfoTeacher>();

        List<InfoTeacher> infoTeachers = getBaseMapper().selectList(
                wrapper.ne(InfoTeacher::getLevel, EnumLevel.Admin)
                        .orderByAsc(InfoTeacher::getLevel));

        return infoTeachers;
    }

    public int deleteTeacher(int teacherId){
        return getBaseMapper().deleteById(teacherId);
    }

    public int updateTeacher(InfoTeacher infoTeacher){
        return getBaseMapper().updateById(infoTeacher);
    }

}
